package com.javandad.app.client.home;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.javandad.app.client.cells.HomePostCell;
import com.javandad.app.shared.entity.home.HomePost;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class HomeComponent implements EntryPoint {

	private final HomeServiceAsync homeService = GWT.create(HomeService.class);

	private void initGUI() {

		count = 1;
		RootPanel rootPanel = RootPanel.get("homeContainer");

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setSize("80%", "100%");
		rootPanel.add(verticalPanel);

		VerticalPanel verticalPanel2 = new VerticalPanel();
		verticalPanel2.setSize("100%", "100%");
		postButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				sendPost();
			}
		});

		showMorePosts.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				count++;
				showPosts();
			}
		});
		showMorePosts.setWidth("100%");
		showMorePosts.setVisible(false);

		text.setSize("100%", "70");
		verticalPanel2.add(text);
		verticalPanel2.add(postButton);

		postsList.setWidth("100%");

		verticalPanel.add(verticalPanel2);

		verticalPanel.add(postsList);

		verticalPanel.add(showMorePosts);

		showPosts();
	}

	public void onModuleLoad() {

		getSession();

	}

	private void showPosts() {
		homeService.downloadPosts(currentUser, count, new AsyncCallback<List<HomePost>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<HomePost> result) {
				text.setText("");
				if (result != null)
					postsList.setRowData(result);

				no = result.size();

				if (no % 20 == 0 && no != 0)
					showMorePosts.setVisible(true);
				else
					showMorePosts.setVisible(false);
			}
		});
	}

	private void sendPost() {
		HomePost post = new HomePost(null, currentUser, text.getText(), null);
		homeService.uploadPost(post, new AsyncCallback<HomePost>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(HomePost result) {
				showPosts();
			}
		});
	}

	private void getSession() {
		homeService.getSessionInfo(new AsyncCallback<List<String>>() {

			@Override
			public void onSuccess(List<String> result) {

				session = result;
				view = Window.Location.getParameter("view");
				if (view != null && !view.equals("")) {
					currentUser = view;
					initGUI();
				} else if (session != null && session.get(1) != null && !session.get(1).equals("")) {

					currentUser = session.get(1);
					initGUI();
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("no");
				view = Window.Location.getParameter("view");
				if (view != null && !view.equals("")) {
					currentUser = view;
					initGUI();
				}
				Window.alert("no");
			}

		});
	}

	private TextArea text = new TextArea();
	private Button postButton = new Button("Post");
	private CellList<HomePost> postsList = new CellList<HomePost>(new HomePostCell());
	Button showMorePosts = new Button("...");

	String view = null;
	String currentUser = null;
	List<String> session;
	private int count = 1;
	private int no = 0;
}
